package com.yc.cloud.admin.controller;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.orm.CommonPage;
import com.yc.cloud.common.basic.dto.BasePageReqDTO;
import com.yc.cloud.admin.dto.request.AlgStrategyRuleCreateReqDTO;
import com.yc.cloud.admin.dto.request.AlgStrategyRuleQueryReqDTO;
import com.yc.cloud.admin.dto.request.AlgStrategyRuleUpdateReqDTO;
import com.yc.cloud.admin.vo.AlgStrategyRuleVo;
import com.yc.cloud.admin.service.AlgStrategyRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;

/**
 * <p>
 * 算法策略规则表 前端控制器
 * </p>
 *
 * @author kinggu
 * @since 2025-07-30
 */
@RestController
@Api(tags = "算法策略规则表")
@RequestMapping("/alg-strategy-rules")
public class AlgStrategyRuleController {

    @Resource
    private AlgStrategyRuleService algStrategyRuleService;

    @ApiOperation("创建算法策略规则表")
    @PostMapping
    public ResponseResult<AlgStrategyRuleVo> create(@Validated @RequestBody AlgStrategyRuleCreateReqDTO createReqDTO) {
        AlgStrategyRuleVo newVo = algStrategyRuleService.create(createReqDTO);
        if (newVo != null) {
            return ResponseResult.success(newVo);
        }
        return ResponseResult.failed("创建失败");
    }

    @ApiOperation("更新算法策略规则表")
    @PutMapping("/{id}")
    public ResponseResult<AlgStrategyRuleVo> update(@PathVariable("id") Long id, @Validated @RequestBody AlgStrategyRuleUpdateReqDTO updateReqDTO) {
        updateReqDTO.setId(id);
        AlgStrategyRuleVo updatedVo = algStrategyRuleService.update(updateReqDTO);
        if (updatedVo != null) {
            return ResponseResult.success(updatedVo);
        }
        return ResponseResult.failed("更新失败");
    }

    @ApiOperation("发布策略规则")
    @PutMapping("/{id}/publish")
    public ResponseResult<Boolean> publish(@PathVariable("id") Long id) {
        boolean result = algStrategyRuleService.publish(id);
        return result ? ResponseResult.success(true, "策略规则发布成功") : ResponseResult.failed("策略规则发布失败");
    }

    @ApiOperation("取消发布策略规则")
    @PutMapping("/{id}/unpublish")
    public ResponseResult<Boolean> unpublish(@PathVariable("id") Long id) {
        boolean result = algStrategyRuleService.unpublish(id);
        return result ? ResponseResult.success(true, "策略规则取消发布成功") : ResponseResult.failed("策略规则取消发布失败");
    }

    @ApiOperation("删除算法策略规则表")
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable("id") Long id) {
        boolean success = algStrategyRuleService.delete(id);
        if (success) {
            return ResponseResult.success(true);
        }
        return ResponseResult.failed("删除失败");
    }

    @ApiOperation("获取算法策略规则表详情")
    @GetMapping("/{id}")
    public ResponseResult<AlgStrategyRuleVo> getById(@PathVariable("id") Long id) {
        return ResponseResult.success(algStrategyRuleService.getVOById(id));
    }

    @ApiOperation("分页查询算法策略规则表")
    @GetMapping
    public ResponseResult<CommonPage<AlgStrategyRuleVo>> list(@Validated AlgStrategyRuleQueryReqDTO queryReqDTO) {
        return ResponseResult.success(algStrategyRuleService.list(queryReqDTO));
    }
}
 